package cn.alise.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import org.springframework.format.Formatter;

/**
 * ClassName:DateFormatter <br/>
 * Description: 日期格式化类 <br/>
 * Date: 2015年9月8日 下午4:41:54 <br/>
 *
 * @author zhuyf
 * @version
 * @since JDK 1.7
 * @see
 */
public class DateFormatter implements Formatter<Date> {
	
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String HH_MM = "HH:mm";
	public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm:ss";

	@Override
	public Date parse(String text, Locale locale) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		try {
			date = format.parse(text);
		} catch (Exception e) {
			format = new SimpleDateFormat("yyyy-MM-dd");
			date = format.parse(text);
		}
		return date;
	}
	
	/**
	 * 字符串转日期类型
	 * @param text
	 * @param fmt
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String text, String fmt) throws ParseException {
		return new SimpleDateFormat(fmt).parse(text);
	}
	
	public static String toStr(Date date, String partten) throws ParseException  {
		SimpleDateFormat format = new SimpleDateFormat(partten);
		try {
			return format.format(date);
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static Timestamp getDayTimestamp() throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return new Timestamp(format.parse(format.format(new Date())).getTime());
	}
	
	public static String toStr(Timestamp stp, String partten) throws ParseException  {
		SimpleDateFormat format = new SimpleDateFormat(partten);
		try {
			return format.format(stp);
		} catch (Exception e) {
			throw e;
		}
	}

	public static Timestamp stmp(String text, String partten) throws ParseException {
		SimpleDateFormat format = new SimpleDateFormat(partten);
		try {
			Date date = format.parse(text);
			return new Timestamp(date.getTime());
		} catch (Exception e) {
			throw e;
		}
	}
	
	public static String longToStr(Long stp, String partten) throws ParseException  {
		SimpleDateFormat sdf= new SimpleDateFormat(partten);  
        Date date = new Date(stp);
        
		try {
			return sdf.format(date);
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public String print(Date object, Locale locale) {
		return null;
	}

}
